package com.example.demo.config;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import com.example.demo.common.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
//@ResponseBody
@ResponseBody
@Component
@RequestMapping("/captcha")
public class Captcha {
    @Autowired
    HttpSession session;

    /*
        生成横线阻挡的验证码
     */
    @RequestMapping("/verify")
    public void verify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //验证码尺寸的大小
        int width = 158;
        int height = 40;
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(width, height, 5, 4);
        //图形验证码写出，可以写出到文件，也可以写出到流
        shearCaptcha.write(response.getOutputStream());
        //获取验证码中的文字内容 存储到session里
        request.getSession().setAttribute("verifyCode", shearCaptcha.getCode());
    }
    /*
        生成圆圈阻挡的验证码
     */
    @GetMapping("/code")
    void getCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
        // 利用 hutool 工具，生成验证码图片资源
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        // 获得生成的验证码字符
        String code = captcha.getCode();
        // 利用 session 来存储验证码
        session.setAttribute("code",code);
        // 将验证码图片的二进制数据写入【响应体 response 】
        captcha.write(response.getOutputStream());
    }
    /**
     * session存储   用来校验注册的验证码输入是否匹配
     *
     * @param captchaCode 验证码
     * @param request     HttpServletRequest
     * @return boolean  用户输入的和系统生成的验证码一致 返回ture 否则 false
     */
    @RequestMapping("/checkCaptcha")
    public AjaxResult  getCheckCaptcha(String captchaCode, HttpServletRequest request) {
        try {
            System.out.println("进行验证码验证");
            // toLowerCase() 不区分大小写进行验证码校验.
            //获取之前生成的验证码
            String sessionCode = String.valueOf(request.getSession().getAttribute("verifyCode")).toLowerCase();
            System.out.println("session里的验证码：" + sessionCode);
            //得到用户输入的验证码
            String receivedCode = captchaCode.toLowerCase();
            System.out.println("用户的验证码：" + receivedCode);
            if(!"".equals(sessionCode) && !"".equals(receivedCode) && sessionCode.equals(receivedCode)){
                System.out.println("验证通过");
                return AjaxResult.success(1);
            }else {
                return AjaxResult.fail(-1,"验证码错误");
            }
        } catch (Exception e) {
            return AjaxResult.fail(-1,"验证码错误");

        }
    }

}
